package com.demo.search.spring.webapp;

import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;

import com.demo.common.ClassLoaderUtils;
import com.demo.search.spring.IndexWriterFactoryBean;

/**
 * 索引写入类的创建工厂
 * 创建web-app系统下的索引写入对象(文件相对路径:WEB-INF/xx)
 * @author ryuu.kk
 *
 */
public class WebIndexWriterFactoryBean extends IndexWriterFactoryBean implements ServletContextAware {

	private ServletContext servletContext;
	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	protected String getRealPath(String path) {
		path = ClassLoaderUtils.getRealPath(path);
		// WEB-INF
		if (!path.startsWith("/" )) {
			path = "/" + path;
		}
		return servletContext.getRealPath(path);
	}
}